/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.pdx.cs.guavadata.query;

import edu.pdx.cs.guavadata.Channel;
import edu.pdx.cs.guavadata.Transform;
import edu.pdx.cs.guavadata.condition.Condition;
import edu.pdx.cs.guavadata.subclass.ConditionType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author VuTran
 */
public class Delete extends Operator {

    // DML Delete Statement
    
    String table;
    List<String> columns;
    List<Condition> conditions;
    
    public List<String> getColumns() {
        return columns;
    }

    public void setColumns(List<String> columns) {
        this.columns = columns;
    }

    public List<Condition> getConditions() {
        return conditions;
    }

    public void setConditions(List<Condition> conditions) {
        this.conditions = conditions;
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public Delete(String table) {
        this.table = table;
        this.columns = new ArrayList<String>();
        this.conditions = new ArrayList<Condition>();
        this.parent = null;
    }

    public Delete(String table, Collection<String> columns, Collection<Condition> conditions) {
        this.table = table;
        this.columns = new ArrayList<String>(columns);
        this.conditions = new ArrayList<Condition>(conditions);
        this.parent = null;

        try {
            // Dummy check
            if (this.columns.size() != this.conditions.size())
                throw new Exception("Number of columns must equal the number of conditions.");

            // Check all of the conditions are equality conditions
            for (Condition c : this.conditions) {
                if (c.getCondType() != ConditionType.EQ)
                    throw new Exception("Found a condition of type " + c.getCondType().toString() + " in a delete statement - only equality conditions are accepted.");
            }
        } catch (Exception ex) {
            System.err.println("ERROR: " + ex.toString());
        }
    }

    public void addCondition(String column, Condition condition) {
        try {
            if (condition.getCondType() != ConditionType.EQ)
                throw new Exception("Found a condition of type " + condition.getCondType().toString() + " in a delete statement - only equality conditions are accepted.");
        } catch (Exception ex) {
            System.err.println("ERROR: " + ex.toString());
        }
        this.columns.add(column);
        this.conditions.add(condition);
    }

    @Override
    public boolean isQuery() {
        return false;
    }

    @Override
    public SqlText visit(Provider qv) {
        return qv.transform(this);
    }

    @Override
    public void visit(Channel c, String table) {
        c.mapAliases(this, table);
    }

    @Override
    public void visit(Transform t, Command c) {
        t.expand(this, c);
    }

    @Override
    public void notifyChange(Operator old, Operator newOp) {
        throw new UnsupportedOperationException("The method or operation is not implemented.");
    }

    @Override
    public Operator clone() {
        return new Delete(table, columns, conditions);
    }

}
